<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.3.4/vue.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="root">
        <h2>用户列表</h2>
        <input type="text" placeholder="请输入用户名" v-model="keyword">
        <button type="button" @click="goTo">新增用户</button>
        <ul>
            <li v-for="per in users" :key="per.username">
                用户名：{{per.username}} - 年龄：{{per.age}} -- 性别：{{per.gender}}
            </li>
        </ul>
    </div>

    <script>
        let ref = Vue.ref;
        const app = Vue.createApp({
            setup(props) {
                let keyword = Vue.ref('');
                let user = ref([]);
                let goTo = function name(params) {
                    location.href = '/Vue/addUser.html';
                };
                let users =  Vue.computed(() => {
                    return user.value.filter( u => {
                            return u.username.indexOf(keyword.value) != -1;
                        })
                });

             // 发送异步请求获取用户列表
                const loadUsers = async () => {
                try {
                    const response = await axios.get('http://localhost:8080/user');
                    user.value = response.data;
                } catch (error) {
                    console.log(error);
                }
                };

                loadUsers();

                // 监听关键字的变化
                Vue.watch(keyword, () => {
                console.log('Keyword changed:', keyword.value);
                });

                return {keyword,goTo,user,users,loadUsers}
            }
        }).mount('#root')
    </script>
</body>
</html>